import ACTIONS from "./action"
// 定义一个函数evaluate，用于计算两个操作数的结果
const evaluate=state=>{
    // 从state中解构出lastOperand、currentOperand和operation
    let { lastOperand, currentOperand, operation } = state;
    // 将lastOperand和currentOperand转换为浮点数
    let last = parseFloat(lastOperand);
    let current = parseFloat(currentOperand);
    
    // 定义一个变量res，用于存储计算结果
    let res = "";
    // 根据operation的值，进行相应的计算
    switch (operation) {
        case '+':
            // 如果operation为'+'，则将last和current相加
            res = last + current;
            break;
        case '-':
            // 如果operation为'-'，则将last和current相减
            res = last - current;
            break;
        case '*':
            // 如果operation为'*'，则将last和current相乘
            res = last * current;
            break;
        case '/':
            // 如果operation为'/'，则将last和current相除
            res = last / current;  
            break;
    }
    // 将计算结果转换为字符串并返回
    return res.toString();

}
const reducer = (state = {
    currentOperand: "",
    lastOperand: "",
    operation: "",
    overwrite: false,
}, action) => {
    switch (action.type) {
        case ACTIONS.ADD_DIGIT:
            if (state.overwrite)
                return {
                    ...state,
                    currentOperand: action.digit,
                    overwrite: false
                }
            if (state.currentOperand === '0' && action.digit === '0')
                return state;
            if (state.currentOperand === '0' && action.digit !== '.')
                return {
                ...state,
                currentOperand: action.digit,
                }
            if (action.digit === '.' && state.currentOperand.includes('.'))
                return state;
            if (action.digit === '.' && state.currentOperand === '')
                return {
                    ...state,
                    currentOperand: '0.',
                }
            return {
                ...state,
                currentOperand: state.currentOperand+action.digit,
            }
        case ACTIONS.DELETE_DIGIT:
            if (state.overwrite)
                return {
                    ...state,
                    currentOperand: '',
                    overwrite: false,
                }
            if (state.currentOperand === '')
                return state;
            return {
                ...state,
                currentOperand: state.currentOperand.slice(0, -1),
            }
        case ACTIONS.CHOOSE_OPERATION:
            if (state.lastOperand === "" && state.currentOperand === "")
                return state;
            if (state.lastOperand === "" )
                return {
                    ...state,
                    lastOperand: state.currentOperand,
                    operation: action.operation,
                    currentOperand: "",
                }
            if (state.currentOperand === "")
                return {
                    ...state,
                    operation: action.operation,
                }
            return {
                ...state,
                lastOperand: evaluate(state),
                operation: action.operation,
                currentOperand: "",
            }
        case ACTIONS.CLEAR:
            return {
                ...state,
                currentOperand: "",
            }
        case ACTIONS.EVALUATE:
            if (state.operation === "" ||
                state.currentOperand === "" ||
                state.lastOperand === "")
                return state;
            return {
                ...state,
                currentOperand: evaluate(state),
                lastOperand: "",
                operation: "",
                overwrite: true,
            }
                
        default:
            return state;
    }
}

export default reducer;